﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.IO;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.YouTube;

namespace YoutubeVideoManager
{
    /// <summary>
    /// Logica di interazione per PlaylistItem.xaml
    /// </summary>
    public partial class PlaylistItem : UserControl
    {
        #region Event
        public event EventHandler DownloadCompleted;
        #endregion

        #region Properties
        public Playlist Playlist { get; private set; }
        public bool IsDownloading { get; private set; }
        public Video[] Videos { get { return videos.ToArray(); } }
        #endregion

        #region Fields
        BackgroundWorker worker;
        List<Video> videos;
        #endregion

        #region Methods
        public PlaylistItem(Playlist playlist)
        {
            InitializeComponent();

            this.IsDownloading = true;
            this.Playlist = playlist;

            videos = new List<Video>();

            worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync();
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            byte[] data = e.Result as byte[];
            if (data == null)
                return;
            
            BitmapImage image = new BitmapImage();
            using (MemoryStream ms = new MemoryStream(data))
            {
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = ms;
                image.EndInit();
                image.Freeze();
                if (this.img.Source == null)
                    this.img.Source = image;
            }

            this.img.Source = image;
            this.txt.Text = Playlist.Title;
            
            this.IsDownloading = false;
            if (DownloadCompleted != null)
                DownloadCompleted(this, EventArgs.Empty);
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            byte[] data = null;
            Feed<PlayListMember> feed = Global.Request.GetPlaylist(this.Playlist);
            foreach (Video video in feed.Entries)
            {
                videos.Add(video);
                if (data == null && video.Thumbnails.Count > 0)
                {
                    using (WebClient client = new WebClient())
                    {
                        data = client.DownloadData(video.Thumbnails[0].Url);
                    }
                }
            }
            e.Result = data;
        }

        #endregion
    }
}
